package cn.xq.pethome.employee.controller;

import cn.xq.pethome.employee.domain.Employee;
import cn.xq.pethome.employee.query.EmployeeQuery;
import cn.xq.pethome.employee.service.IEmployeeService;
import cn.xq.pethome.utils.AjaxUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "employee",description = "顾客相关的CRUD功能")
@RequestMapping("/emp")
public class EmployeeController {
    @Autowired
    private IEmployeeService iEmployeeService;

    @GetMapping ("/list")
    @ApiOperation(value = "查询所有顾客")
    public AjaxUtil fingall(){
        List<Employee> fingall = iEmployeeService.fingall();
        return AjaxUtil.success().setData(fingall);
    }

    @PostMapping("/{id}")
    @ApiOperation(value = "根据id查询顾客")
    public AjaxUtil findone(@PathVariable("id") Long id){
        Employee fingone = iEmployeeService.fingone(id);
        return AjaxUtil.success().setMsg("查询成功").setData(fingone);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除顾客")
    public AjaxUtil delete(@PathVariable("id") Long id){
        iEmployeeService.delete(id);
        return AjaxUtil.success().setMsg("删除成功");
    }

    @PatchMapping("/batchdel")
    @ApiOperation(value = "根据list批量删除顾客")
    public AjaxUtil batchdel(@RequestBody List<Long> ids){
        iEmployeeService.batchDelete(ids);
        return AjaxUtil.success().setMsg("批量删除成功");
    }

    @PostMapping("/pagelist")
    @ApiOperation(value = "高级查询")
    public AjaxUtil findquery(@RequestBody EmployeeQuery employeeQuery){
        try {
            List<Employee> findquery = iEmployeeService.findquery(employeeQuery);
            //Long total=Long.parseLong("findquery.size()");
            Long fingquerycount = iEmployeeService.fingquerycount(employeeQuery);
            return  AjaxUtil.success().setTotal(fingquerycount).setData(findquery);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxUtil.error();
        }
    }
    @PutMapping("/")
    @ApiOperation(value = "增加与修改")
    public AjaxUtil save(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                iEmployeeService.insert(employee);
                return AjaxUtil.success().setMsg("添加成功");
            }else{
                iEmployeeService.update(employee);
                return AjaxUtil.success().setMsg("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxUtil.error();
    }
}
